package com.fone.player.activity.search.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.client.SearchRst.Cnt;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class GridViewAdapter extends BaseAdapter {
	private List<Cnt> mList;
	private Context mContext;
	private int length = 0;

	DisplayImageOptions options;
	
	public GridViewAdapter(Context context, List<Cnt> list) {
		this.mContext = context;
		this.mList = list;
		if(mList!=null){
			length = mList.size();
		}else{
			length = 0;
		}
		
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.default_220_292)
		.showImageForEmptyUri(R.drawable.default_220_292)
		.showImageOnFail(R.drawable.default_220_292)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.build();
		
	}

	public void setData(List<Cnt> mList){
		this.mList = mList;
		if(mList!=null){
			length = mList.size();
		}else{
			length = 0;
		}
	}
	
	@Override
	public int getCount() {
		return length;
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.search_result_gridview_item, null);
			viewHolder.imageView = (ImageView) convertView
					.findViewById(R.id.imageview);
			viewHolder.vipIcon = (ImageView) convertView
					.findViewById(R.id.vip_icon_iv);
			viewHolder.textView = (TextView) convertView
					.findViewById(R.id.textview);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		Cnt cnt = mList.get(position);
		viewHolder.textView.setText(cnt.name);
		ImageLoader.getInstance().displayImage(cnt.pic1,viewHolder.imageView, options);
//		ImageDownloadModule.getInstance().download(cnt.pic1,viewHolder.imageView);
		if (cnt.ftv == 0) {
			viewHolder.vipIcon.setVisibility(View.GONE);
		} else {
			viewHolder.vipIcon.setVisibility(View.VISIBLE);
		}
		return convertView;
	}

	static class ViewHolder {
		public ImageView imageView, vipIcon;
		public TextView textView;
	}
}
